﻿Imports System.Text

Namespace OpenCL
    ''' <summary>
    ''' デバイス
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Device

#Region "　デバイスID　"
        ''' <summary>デバイスID</summary>
        Private _ID As IntPtr = IntPtr.Zero
        ''' <summary>デバイスID</summary>
        Public ReadOnly Property ID() As IntPtr
            Get
                Return Me._ID
            End Get
        End Property
#End Region

#Region "　デバイス名　"
        ''' <summary>デバイス名</summary>
        Private _Name As String = ""
        ''' <summary>デバイス名</summary>
        Public ReadOnly Property Name() As String
            Get
                Return Me._Name
            End Get
        End Property
#End Region

#Region "　ベンダー情報　"
        ''' <summary>ベンダー情報</summary>
        Private _Vendor As String = ""
        ''' <summary>ベンダー情報</summary>
        Public ReadOnly Property Vendor() As String
            Get
                Return Me._Vendor
            End Get
        End Property
#End Region

#Region "　デバイスバージョン　"
        ''' <summary>デバイスバージョン</summary>
        Private _Version As String = ""
        ''' <summary>デバイスバージョン</summary>
        Public ReadOnly Property Version() As String
            Get
                Return Me._Version
            End Get
        End Property
#End Region

#Region "　デバイスドライババージョン　"
        ''' <summary>デバイスドライババージョン</summary>
        Private _DriverVersion As String = ""
        ''' <summary>デバイスドライババージョン</summary>
        Public ReadOnly Property DriverVersion() As String
            Get
                Return Me._DriverVersion
            End Get
        End Property
#End Region

#Region "　デバイスプロファイル　"
        ''' <summary>デバイスプロファイル</summary>
        Private _Profile As String = ""
        ''' <summary>デバイスプロファイル</summary>
        Public ReadOnly Property Profile() As String
            Get
                Return Me._Profile
            End Get
        End Property
#End Region

#Region "　拡張情報　"
        ''' <summary>拡張情報</summary>
        Private _Extensions As String = ""
        ''' <summary>拡張情報</summary>
        Public ReadOnly Property Extensions() As String
            Get
                Return Me._Extensions
            End Get
        End Property
#End Region

#Region "　コンストラクタ　"
        ''' <summary>コンストラクタ</summary>
        ''' <param name="id">プラットフォームのID</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal id As IntPtr)
            Me._ID = id                                                          'デバイスID
            Me._Name = Me.getDeviceInfo(DeviceInfoString.Name)                   'デバイス名
            Me._Vendor = Me.getDeviceInfo(DeviceInfoString.Vendor)               'ベンダー情報
            Me._Version = Me.getDeviceInfo(DeviceInfoString.Version)             'デバイスバージョン
            Me._Profile = Me.getDeviceInfo(DeviceInfoString.Profile)             'デバイスプロファイル
            Me._Extensions = Me.getDeviceInfo(DeviceInfoString.Extensions)       '拡張情報
            Me._DriverVersion = Me.getDeviceInfo(DeviceInfoString.DriverVersion) 'ドライババージョン
        End Sub
#End Region

#Region "　デバイス情報取得　"
        ''' <summary>
        ''' デバイス情報取得
        ''' </summary>
        ''' <param name="deviceInfoName">取得するデバイス情報</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function getDeviceInfo(ByVal deviceInfoName As DeviceInfoString) As String
            Dim valueSize As Integer
            OpenCLAPI.clGetDeviceInfo(Me.ID, deviceInfoName, 0, Nothing, valueSize)
            Dim result As New System.Text.StringBuilder(valueSize)
            OpenCLAPI.clGetDeviceInfo(Me.ID, deviceInfoName, valueSize, result, valueSize)
            Return result.ToString()
        End Function
#End Region

#Region "　ToString　"
        ''' <summary>
        ''' ToString
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Function ToString() As String
            With New StringBuilder
                .AppendLine(String.Format("ID：{0}", Me.ID))
                .AppendLine(String.Format("プラットフォーム名：{0}", Me.Name))
                .AppendLine(String.Format("ベンダー情報：{0}", Me.Vendor))
                .AppendLine(String.Format("プラットフォームのバージョン：{0}", Me.Version))
                .AppendLine(String.Format("プラットフォームのプロファイル：{0}", Me.Profile))
                .AppendLine(String.Format("拡張情報：{0}", Me.Extensions))
                Return .ToString
            End With
        End Function
#End Region

    End Class

    ''' <summary>
    ''' デバイスを格納するコレクション
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DeviceCollection
        Inherits System.Collections.Generic.List(Of Device)

#Region "　コンストラクタ　"
        Public Sub New()
            Me._IDList = New List(Of IntPtr)
        End Sub
#End Region

#Region "　デバイスの読み込み　"
        ''' <summary>
        ''' デバイスの読み込み
        ''' </summary>
        ''' <param name="plat">プラットフォーム</param>
        ''' <remarks></remarks>
        Public Sub Load(ByVal plat As Platform)
            Dim deviceCount As Integer
            OpenCLAPI.clGetDeviceIDs(plat.ID, DeviceType.All, 0, Nothing, deviceCount)

            Dim result As IntPtr() = New IntPtr(deviceCount - 1) {}
            OpenCLAPI.clGetDeviceIDs(plat.ID, DeviceType.All, deviceCount, result, deviceCount)

            For Each deviceID As IntPtr In result
                If (Not Me._IDList.Contains(deviceID)) Then
                    Me._IDList.Add(deviceID)
                End If
                Me.Add(New Device(deviceID))
            Next
        End Sub
#End Region

#Region "　IDリスト　"
        ''' <summary>IDリスト</summary>
        Private _IDList As List(Of IntPtr)
        ''' <summary>IDリスト</summary>
        Public ReadOnly Property IDList() As List(Of IntPtr)
            Get
                Return Me._IDList
            End Get
        End Property
#End Region

    End Class
End Namespace
